const express = require("express")
const {con,getMysql} = require('../../mysql/mysql.js'); 
const admin = express.Router() //创建路由

//判断是否登录
function isLogin(req,res,next){
    if(req.session.username==null){
        res.send({status:404,data:"还未登录，请登录"})
        return
    }
    next()
}

//搜索
admin.get("/search",async (req,res)=>{
    let {message}=req.query
    let str=`select * from book where author like '%${message}%' or bookname like '%${message}%'` 
    let data=await getMysql(str)
    res.send({status:200,data:data})
})

//获取首页推荐数据
admin.get("/shouyetuijian",async (req,res)=>{
    let page=1         //第几页
    let pageNumber=6           //每页几条
    let arr=[(page-1)*pageNumber,pageNumber]
    let str1="select * from article limit ?,?"
    let data1=await getMysql(str1,arr)
    res.send({status:200,data:data1})
})



//获取分类推荐数据
admin.get("/",async (req,res)=>{
    let str1="select * from book where catagory='玄幻小说' limit ?,?"
    let str2="select * from book where catagory='修真小说'  limit ?,?"
    let str3="select * from book where catagory='都市小说'  limit ?,?"
    let str4="select * from book where catagory='穿越小说'  limit ?,?"
    let str5="select * from book where catagory='网游小说'  limit ?,?"
    let str6="select * from book where catagory='科幻小说'  limit ?,?"
    let page=3          //第几页
    let pageNumber=6           //每页几条
    let arr=[(page-1)*pageNumber,pageNumber]
    let data1=await getMysql(str1,arr)
    let data2=await getMysql(str2,arr)
    let data3=await getMysql(str3,arr)
    let data4=await getMysql(str4,arr)
    let data5=await getMysql(str5,arr)
    let data6=await getMysql(str6,arr)
    let data=[...data1,...data2,...data3,
        ...data4,...data5,...data6]
    res.send({status:200,data:data})
})

//获取各个分类的小说书籍
admin.get("/catagory",async (req,res)=>{
    let {page,pageNumber,catagoryname}=req.query

    let str1=`select * from book where catagory=? limit ?,?`
    let arr=[catagoryname,(page-1)*(pageNumber-0),pageNumber-0]
    let data1=await getMysql(str1,arr)

    let arr2=[catagoryname]
    let str2=`select * from book where catagory=?`
    let data2=await getMysql(str2,arr2)
    let total=data2.length
    // console.log(total)

    res.send({status:200,total:total,data:data1})
})

module.exports = admin
